<template>
  <div class="teacher">
    <!-- 学院模块 -->
    <el-container>
      <el-header>
        <h1>攀枝花学院毕业设计管理系统(学校管理模块)</h1>
        <div>
          <span
            ><router-link to="/bigadmin_info">{{
              msg.name + "(" + msg.num + ")"
            }}</router-link></span
          >
          <el-button type="primary" round @click="updatePwd()"
            >修改密码
          </el-button>
          <el-button type="primary" round @click="loginout()">退出</el-button>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
            :router="true"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
          >
            <el-menu-item index="collage_info">
              <i class="el-icon-menu"></i>
              <span slot="title">学校信息</span>
            </el-menu-item>
            <el-menu-item index="checks">
              <i class="el-icon-menu"></i>
              <span slot="title">毕业设计</span>
            </el-menu-item>
            <el-menu-item index="collage_paper">
              <i class="el-icon-menu"></i>
              <span slot="title">学校的毕设情况</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>

    <!-- 修改密码弹框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="旧密码" prop="old">
          <el-input
            type="text"
            v-model="ruleForm.old"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input
            type="text"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="text"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="true_updatePwd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.get_bigAdminInfo();
  },
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      msg: {},
      ruleForm: {},
      rules: {
        old: [{ min: 6, max: 12, message: "长度为6-12", trigger: "blur" }],
        pass: [
          { validator: validatePass, trigger: "blur" },
          { min: 6, max: 12, message: "长度为6-12", trigger: "blur" },
        ],
        checkPass: [
          { validator: validatePass2, trigger: "blur" },
          { min: 6, max: 12, message: "长度为6-12", trigger: "blur" },
        ],
      },
      dialogVisible: false,
    };
  },
  methods: {
    loginout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },

    updatePwd() {
      this.dialogVisible = true;
    },
    true_updatePwd() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return;

        this.$http
          .post(
            "http://127.0.0.1:8080/bigAdmin/update_pwd",
            this.$qs.stringify(this.ruleForm)
          )
          .then((res) => {
            if (res.data.status == 0) {
              this.$message.success(res.data.message);
              window.sessionStorage.clear;
              this.$router.push("/login");
            } else {
              this.$message.error(res.data.message);
            }
          })
          .catch((err) => {
            this.$message.error(err);
          });
      });
      // console.log(this.ruleForm);
    },

    get_bigAdminInfo() {
      this.$http
        .get("http://127.0.0.1:8080/bigadmin/get_bigAdminInfo")
        .then((res) => {
          this.msg = res.data.data[0];
          // console.log(this.msg);
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
  },
};
</script>
<style lang="less" scoped>
.el-container,
.teacher {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
}
.el-header a {
  color: white;
  text-decoration: none;
}

.el-aside {
  background-color: #333743;
}
.el-main {
  background-color: #eaedea;
}
</style>
